import { defineStore } from 'pinia'
import scanSeries from '@renderer/store/types/ScanSeries'
import { postWithAns } from '@renderer/api/http'
import API from '@renderer/api/constant'
import { patientInfoType } from '@renderer/types'

const useScanStore = defineStore('scan', {
  state: () => {
    return {
      scanSeriesInfo: scanSeries,
      imageShowSizeMap: new Map(),
      activeReconSeriesId: null,
      scanPatientInfo: null,
      scanPosture: '',
      keepAliveArr: [] as string[]
    }
  },
  getters: {
    keepAliveRouteArr: (state) => {
      return state.keepAliveArr
    },
    scanPatientShowInfoArr: (state) => {
      if (state.scanPatientInfo === null) {
        return []
      }
      const { patientName, patientId, age, gender, height, patientIndex, weight } = state.scanPatientInfo as any
      let genderStr = 'O'
      if (gender === 0) {
        genderStr = 'M'
      } else if (gender === 1) {
        genderStr = 'F'
      }
      const row1 = `${patientName ? patientName : ''},${patientId ? 'ID:' + patientId : ''},${patientIndex ? 'No.' + patientId : ''}`
      const row2 = `${genderStr}, ${age ? `${age},` : ''}${height ? `${height},` + 'cm' : ''}${weight ? `${weight},` + 'kg' : ''}`
      return [row1, row2]
    }
  },
  actions: {
    setImageShowSize(imageId, imageShowSize) {
      this.imageShowSizeMap.set(imageId, imageShowSize)
    },
    updateScanSeries(newScanSeries) {
      scanSeries.updateScanSeries(newScanSeries)
    },
    setActiveReconSeriesId(newActiveReconSeriesId) {
      this.activeReconSeriesId = newActiveReconSeriesId
    },
    appendScanSeries(payload) {
      scanSeries.appendScanSeries(payload)
    },

    relate2ScanSeries(prevSeriesId, nextSeriesId) {
      scanSeries.relate2ScanSeries(prevSeriesId, nextSeriesId)
    },
    setScanPatientInfo(patientInfo) {
      this.scanPatientInfo = patientInfo
    },
    setScanPosture(posture) {
      this.scanPosture = posture
    },
    setKeepAliveRouteArr(routeName: string) {
      if (!this.keepAliveArr.includes(routeName)) {
        this.keepAliveArr.push(routeName)
      }
    },
    createPatientInfo(patientInfo: patientInfoType, createType: 'add' | 'emergencyAdd') {
      const fillPatientInfo = (patientInfo: patientInfoType, createType: 'add' | 'emergencyAdd') => {
        if (createType === 'add') {
          if (patientInfo.patientName === '') {
            patientInfo.patientName = '匿名患者'
          }
        } else if (createType === 'emergencyAdd') {
          if (patientInfo.patientName === '') {
            patientInfo.patientName = '急诊患者'
          }
        }
      }

      const createSuccessFn = (ans, resolve: any) => {
        const { patientIndex, patientId } = ans
        patientInfo.patientId = patientId
        patientInfo.patientIndex = patientIndex
        fillPatientInfo(patientInfo, createType)
        this.setScanPatientInfo(patientInfo)
        resolve(patientInfo)
      }

      return new Promise((resolve, reject) => {
        postWithAns(API.addPatient,
          (ans) => createSuccessFn(ans, resolve),
          patientInfo,
          (msg) => reject(msg))
      })


    }
  }

})

export {
  useScanStore
}

